const {
	addAPIMethod,
	addModuleMethod,
	delTreeMethod,
	showTreeMenu,
	setModuleNameMethod,
	loadAPIDetailMethod,
	addChildModuleMethod,
} = require('./api-module-tree-methods-2x')
const {
	$U_UserInfo,
} = require('@utils/storage')

/**
 * 初始化左侧接口树形菜单
 * @param  {[type]} apiComp      [description]
 * @return {[type]}              [description]
 */
module.exports = function initModuleTree(apiComp) {
	let {
		treeComp,
		treeMenuComp,
	} = apiComp

	// 右键菜单,只有"模块"显示
	treeMenuComp.menu('appendItem', {
		id: 'addApi',
		text: '添加接口',
		iconCls: 'tree-file',
		onclick(e) {
			addAPIMethod.call(apiComp, e)
		},
	}).menu('appendItem', {
		id: 'addModule',
		text: '添加模块',
		iconCls: 'tree-folder',
		onclick(e) {
			addModuleMethod.call(apiComp, e)
		},
	}).menu('appendItem', {
		id: 'addChildModule',
		text: '添加子模块',
		iconCls: 'tree-folder-open',
		onclick(e) {
			addChildModuleMethod.call(apiComp, e)
		},
	}).menu('appendItem', {
		id: 'delete',
		text: '删除',
		iconCls: 'icon-cancel',
		onclick(e) {
			delTreeMethod.call(apiComp, e)
		},
	})

	let userInfo = $U_UserInfo();
	treeComp.tree({
		dnd: true,
		animate: true,
		// 右键显示模块操作菜单,只有模块可以显示
		onContextMenu(e, node) {
			showTreeMenu.call(apiComp, e, node)
		},
		// 双击开始修改模块名称
		onDblClick(node) {
			if (!apiComp.store.isLock)
				return false
			treeComp.tree('beginEdit', node.target);
			// 变更前节点名称
			apiComp.store.tmpNodeName = node.text
			apiComp.store.beginEditName = true
			apiComp.store._currentTreeNode = node
			return true
		},
		// 修改模块名称
		onAfterEdit(node) {
			setModuleNameMethod.call(apiComp, node)
		},

		onClick(node) {
			loadAPIDetailMethod.call(apiComp, node)
		},
	})
}